package com.boding.power.intercept;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class SysLogInterceptor implements Interceptor {

	public void intercept(Invocation inv) {
		HttpServletRequest request = inv.getController().getRequest();
		String url = inv.getActionKey();
		Subject subject = SecurityUtils.getSubject();

		if (url.startsWith("/admin/") && subject != null) {
			String ip = request.getRemoteAddr();
			String method = request.getMethod();
			String browser = request.getHeader("User-Agent");
			String host = request.getRemoteHost();
			Record record = new Record();
			record.set("ip", ip);
			record.set("url", url);
			record.set("method", method);
			record.set("browser", browser);
			record.set("host", host);
			record.set("create_time", new Date());
			record.set("user_name", subject.getPrincipal());
			Db.save("sys_log", record);
		} 
		inv.invoke();
	}
}
